package database

import "salary/model"

/*
 *普通雇员和会员
*/

type PayrollDatabase struct {
	employees    	map[int]*model.Employee
	unionMembers 	map[int]int
}

var Database = &PayrollDatabase{	map[int]*model.Employee{},
										map[int]int{}}

func (pd *PayrollDatabase) GetEmployee(empid int) *model.Employee {
	return pd.employees[empid]
}

func (pd *PayrollDatabase) AddEmployee(empid int, employee model.Employee) {
	pd.employees[empid] = &employee
}

func (pd *PayrollDatabase) Clear() {
	pd.unionMembers = map[int]int{}
	pd.employees = map[int]*model.Employee{}
}

func (pd *PayrollDatabase) DeleteEmployee(empid int) {
	delete(pd.employees, empid)
}

func (pd *PayrollDatabase) AddUnionMember(memberId int, employee *model.Employee) {
	empid := employee.GetEmpid()
	pd.unionMembers[memberId] = empid
}

func (pd *PayrollDatabase) RemoveUnionMember(memberId int) {
	delete(pd.unionMembers, memberId)
}

func (pd *PayrollDatabase) GetUnionMember(memberId int) *model.Employee {
	if empid, ok := pd.unionMembers[memberId]; ok {
		return pd.employees[empid]
	}
	return nil
}

func (pd *PayrollDatabase) GetAllEmployee() []int {
	var list []int
	for key := range pd.employees {
		list = append(list, key)
	}
	return list
}
